<?php

namespace iBrand\Component\Discount\Distributors;

use iBrand\Component\Goods\Models\Goods;
use iBrand\Shop\Core\Models\O2oGoods;

class IntegerDistributor
{

    public function distributePercentage($amount, $subject)
    {
        $itemsTotals = [];
        foreach ($subject->getItems() as $item) {
            $itemsTotals[] = $item->getTotal();
        }

        $total = array_sum($itemsTotals);
        $distributedAmounts = [];

        foreach ($itemsTotals as $element) {
            $distributedAmounts[] = (int)round(($element * $amount) / $total, 0, PHP_ROUND_HALF_DOWN);
        }

        $missingAmount = $amount - array_sum($distributedAmounts);
        for ($i = 0; $i < abs($missingAmount); $i++) {
            $distributedAmounts[$i] += $missingAmount >= 0 ? 1 : -1;
        }

        return $distributedAmounts;
        /*$splitDiscountAmount = [];
        $numberOfTargets = $subject->countItems();
        $percentageTotal = 100;
        $i = 1;
        $items = $subject->getItems();
        foreach ($items as $item) {
            if ($i > $numberOfTargets) {
                break;
            }
            if ($i == $numberOfTargets) {
                $percentageItem = $percentageTotal;
            } else {
                $percentageItem = (int) ($item->units_total / $subject->items_total * 100);
                $percentageTotal -= $percentageItem;
            }
            $splitDiscountAmount[] = (int) ($amount * $percentageItem / 100);
            $i++;
        }
        return $splitDiscountAmount;*/
    }

    public function distribute($amount, $numberOfTargets)
    {
        if (!$this->validateNumberOfTargets($numberOfTargets)) {
            throw new \InvalidArgumentException('数量必须为整数类型，并且大于 0');
        }

        $sign = $amount < 0 ? -1 : 1;
        $amount = abs($amount);

        $low = (int)($amount / $numberOfTargets);
        $high = $low + 1;

        $remainder = $amount % $numberOfTargets;
        $result = [];

        for ($i = 0; $i < $remainder; ++$i) {
            $result[] = $high * $sign;
        }

        for ($i = $remainder; $i < $numberOfTargets; ++$i) {
            $result[] = $low * $sign;
        }

        return $result;
    }

    private function validateNumberOfTargets($numberOfTargets)
    {
        return is_int($numberOfTargets) && 1 <= $numberOfTargets;
    }

    public function distributePercentageShopOrder($amount, $subject, $int_status = true)
    {
        $itemsTotals = [];
        foreach ($subject->getItems() as $item) {
            if (!is_int($item->getTotal() / 100) AND $int_status) {
                $itemsTotals[] = $item->getTotal();
            } elseif (!$int_status) {
                $itemsTotals[] = $item->getTotal();
            }
        }

        if (count($itemsTotals) == 0) {
            foreach ($subject->getItems() as $item) {
                $itemsTotals[] = $item->getTotal();
            }
        }


        $total = array_sum($itemsTotals);
        $distributedAmounts = [];

        foreach ($itemsTotals as $element) {
            $distributedAmounts[] = (int)round(($element * $amount) / $total, 0, PHP_ROUND_HALF_DOWN);
        }

        $missingAmount = $amount - array_sum($distributedAmounts);
        for ($i = 0; $i < abs($missingAmount); $i++) {
            $distributedAmounts[$i] += $missingAmount >= 0 ? 1 : -1;
        }
        return $distributedAmounts;

    }

    public function distributePercentageShopOrderForDouble($amount, $subject)
    {
        $itemsTotals = [];
        foreach ($subject->getItems() as $item) {
            $goods_id = $item->item_meta['detail_id'];
            $goods = O2oGoods::where('goods_id', $goods_id)->where('shop_id', $subject->channel_id)->first();
            if (!$goods->is_discount AND $item->total / $item->units_total > 0.51 AND $goods_id!=492) {
                $itemsTotals[] = $item->getTotal();
            }
        }

        if (count($itemsTotals) == 0) {
            return [];
        }


        $total = array_sum($itemsTotals);
        $distributedAmounts = [];

        foreach ($itemsTotals as $element) {
            $distributedAmounts[] = (int)round(($element * $amount) / $total, 0, PHP_ROUND_HALF_DOWN);
        }

        $missingAmount = $amount - array_sum($distributedAmounts);
        for ($i = 0; $i < abs($missingAmount); $i++) {
            $distributedAmounts[$i] += $missingAmount >= 0 ? 1 : -1;
        }
        return $distributedAmounts;

    }

    public function distributePercentageShopOrderForLevel($amount, $subject)
    {
        $itemsTotals = [];
        foreach ($subject->getItems() as $item) {
            $goods_id = $item->item_meta['detail_id'];

            $baseGoods = Goods::find($goods_id);
            if ($baseGoods->brand_id != 13) //鑫贝尔羽绒服不参与
            {
                $itemsTotals[] = $item->getTotal();
            }
        }

        \Log::info('distributePercentageShopOrderForLevel $itemsTotals:');
        \Log::info($itemsTotals);

        if (count($itemsTotals) == 0) {
            return [];
        }


        $total = array_sum($itemsTotals);
        $distributedAmounts = [];

        foreach ($itemsTotals as $element) {
            $distributedAmounts[] = (int)round(($element * $amount) / $total, 0, PHP_ROUND_HALF_DOWN);
        }

        $missingAmount = $amount - array_sum($distributedAmounts);
        for ($i = 0; $i < abs($missingAmount); $i++) {
            $distributedAmounts[$i] += $missingAmount >= 0 ? 1 : -1;
        }
        return $distributedAmounts;

    }
}
